
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test</title>
</head>

<body>
    <p>
        <button onclick="login()">login</button>
    </p>
    <p>
        <button onclick="getUserInfo()">getUserInfo</button>
        <button onclick="getUserList()">getUserList</button>
    </p>
    <p>
        <button onclick="register()">register</button>
    </p>
    <p>
        <button onclick="delUser()">delUser</button>
    </p>
    <p>
        <button onclick="logout()">logout</button>
    </p>
    <script>
        async function login() {
            const param = { username: 'admin', password: '123456' }
            const result = await fetch('/users/login', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(param)
            }).then(response => response.json());
            console.log(result);
            const t = result.data.token;
            t && localStorage.setItem('token', t);
        }

        async function getUserInfo() {
            const t = localStorage.getItem('token');
            const result = await fetch('/users/userInfo', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + t },
                body: JSON.stringify({})
            }).then(response => response.json())
            console.log(result)
        }

        async function getUserList() {
            const t = localStorage.getItem('token');
            const result = await fetch('/users/userList', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + t },
                body: JSON.stringify({})
            }).then(response => response.json())
            console.log(result)
        }

        async function register() {
            const param = { username: 'admin', password: '123456', email: 'admin@163.com' }
            const result = await fetch('/users/register', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(param)
            }).then(response => response.json())
            console.log(result)
        }

        async function delUser() {
            const t = localStorage.getItem('token');
            const result = await fetch('/users/del', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + t },
                body: JSON.stringify({ id: 999 })
            }).then(response => response.json())
            console.log(result)
        }

        async function logout() {
            const t = localStorage.getItem('token');
            const result = await fetch('/users/logout', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + t },
                body: JSON.stringify({})
            }).then(response => response.json())
            console.log(result)
        }
    </script>
</body>

</html>